import java.util.List;
import java.util.Random;

import com.mongodb.BasicDBObject;
import com.mongodb.client.MongoCollection;

import org.bson.Document;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class mongo {
    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void findOneSound() {

        //Long userId = UserHolder.getUser().getId();

        MongoCollection<Document> collection = mongoTemplate.getCollection("Sound");//获取集合


        BasicDBObject filter_dbobject = new BasicDBObject();
        filter_dbobject.put("userId", 1);//过滤用户
        List<Document> list = (List<Document>) collection.find(filter_dbobject).limit(10);
        Random random = new Random();
        for (int k = 0; k < list.size(); k++) {
            int i = random.nextInt(list.size());
            // 取完后，从目标list内移除该数据
            System.out.println(i);
            list.remove(i);
        }
/*        Query query = new Query();

        Criteria sid = Criteria.where("sid").all();

        Criteria.where("sid").is(sid);

        Sound sound = mongoTemplate.findOne(query, Sound.class);

        return sound;*/
    }


}
